<template>
  <div class="share">
    <Header />
    <div class="main">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="手动派蒙" class="input_text">
          <el-input
            v-model="input"
            placeholder="请输入你想搜索的字"
            @keyup.enter.native="search_action(input)"
          ></el-input>
          <input
            id="hiddenText"
            type="text"
            style="display: none"
            onkeypress="searchKeywordKeyboard(event)"
          />
        </el-form-item>
        <el-form-item class="input_text">
          <el-button
            type="primary"
            @click="search_action(input)"
            icon="el-icon-search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="condition">
        <ul class="cate-list">
          <li
            :class="filter.department == 0 ? 'this' : ''"
            @click="filter.department = 0"
          >
            全部
          </li>
          <li
            :class="filter.department == cate.id ? 'this' : ''"
            v-for="cate in category"
            @click="filter.department = cate.id"
            :key="cate.name"
          >
            {{ cate.name }}
          </li>
        </ul>
      </div>
      <div class="complete-list">
        <div
          class="complete-item"
          v-for="complete in completes_list"
          :key="complete.name"
        >
          <div class="complete-image">
            <img :src="complete.cover" alt />
          </div>
          <div class="complete-info">
            <h3>
              <router-link :to="'/detail/' + complete.id" class="link">
                <span class="title">{{ complete.name }}</span>
              </router-link>
            </h3>
            <Intro class="intro" />
            <p class="sharer-info">
              <span>
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-gangtiexia" />
                </svg>
                {{ complete.sharer.name }}
              </span>
              <span>
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-zhizhuxia" />
                </svg>
                {{ complete.sharer.title }}
              </span>
              <span>
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-leishen" />
                </svg>
                分享时间：{{ complete.release_time }}
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="pagination_block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="filter.page"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="filter.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
        >></el-pagination
      >
    </div>
    <Foot />
  </div>
</template>
<script>
import Header from "../components/Header";
import Foot from "../components/Foot";
import Intro from "../components/Intro";
export default {
  name: "Share",
  components: {
    Header,
    Foot,
    Intro,
  },
  data() {
    return {
      input: "",
      category: [], // 部门分类列表
      completes_list: [], // 分享列表
      count: 0,
      filter: {
        department: 0, // 当前用户选择的部门分类
        ordering: "-id", // 数据的排序方式，默认值是-id，表示对于id进行降序排列
        page_size: 5, // 单页数据量
        page: 1,
      },
    };
  },
  activated() {
    this.get_category();
    this.get_course();
  },
  // mounted() {
  //   this.get_category();
  //   this.get_course();
  // },
  watch: {
    "filter.department": function () {
      this.filter.page = 1;
      this.get_course();
    },
    "filter.page_size": function () {
      this.get_course();
    },
    "filter.page": function () {
      this.get_course();
    },
  },
  methods: {
    handleSizeChange(val) {
      // 每页数据量发生变化时执行的方法
      this.filter.page = 1;
      this.filter.page_size = val;
    },
    handleCurrentChange(val) {
      // 页码发生变化时执行的方法
      this.filter.page = val;
    },
    get_category() {
      this.$axios({
        url: this.$settings.base_url + "/train/categories/",
        method: "get",
      })
        .then((response) => {
          this.category = response.data;
        })
        .catch(() => {
          this.$notify({
            type: "error",
            title: "获取分类信息有误，请联系陈鸿龙",
            duration: 3000,
          });
        });
    },
    get_course() {
      // 排序
      let filters = {
        ordering: this.filter.ordering, // 排序
      };
      // 判决是否进行分类课程的展示
      if (this.filter.department > 0) {
        filters.department = this.filter.department;
      }

      // 设置单页数据量
      if (this.filter.page_size > 0) {
        filters.page_size = this.filter.page_size;
      } else {
        filters.page_size = 5;
      }

      // 设置当前页码
      if (this.filter.page > 1) {
        filters.page = this.filter.page;
      } else {
        filters.page = 1;
      }

      this.$axios({
        url: this.$settings.base_url + "/train/completes/",
        method: "get",
        params: filters,
      })
        .then((response) => {
          this.completes_list = response.data.results;
          this.count = response.data.count;
        })
        .catch(() => {
          this.$notify({
            type: "error",
            title: "获取课程信息有误，请联系陈鸿龙",
            duration: 3000,
          });
        });
    },
    search_action(input) {
      if (!input) {
        this.$notify({
          type: "warning",
          title: "请输入查询内容",
          duration: 3000,
        });
      } else {
        let routeUrl = this.$router.resolve({
          path: "/search",
          query: { key: input },
        });
        window.open(routeUrl.href, "_blank");
      }
      this.input = "";
    },
  },
};
</script>
<style scoped>
.share {
  background: #f6f6f6;
}
.share .main {
  width: 1100px;
  margin: 25px auto 0;
}

.share .condition {
  margin-bottom: 35px;
  padding: 25px 30px 25px 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px 0 #f0f0f0;
}

.share .cate-list::after {
  content: "";
  display: block;
  clear: both;
}

.share .cate-list li {
  font: 18px large;
  font-family: "Microsoft YaHei";
  float: left;
  padding: 6px 15px;
  line-height: 16px;
  margin-left: 14px;
  position: relative;
  transition: all 0.3s ease;
  cursor: pointer;
  color: #4a4a4a;
  border: 1px solid transparent; /* transparent 透明 */
}

.share .cate-list .this {
  color: #ffc210;
  border: 1px solid #ffc210 !important;
  border-radius: 30px;
}

.share .complete-item:hover {
  box-shadow: 4px 6px 16px rgba(0, 0, 0, 0.5);
  border-radius: 10px;
}

.share .complete-item {
  width: 100%;
  background: #fff;
  padding: 20px 30px 20px 20px;
  margin-bottom: 35px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.share .complete-item::after {
  content: "";
  display: block;
  clear: both;
}

/* 顶级元素 父级元素  当前元素{} */
.share .complete-item .complete-image {
  float: left;
  width: 423px;
  height: 210px;
  margin-right: 30px;
}

.share .complete-item .complete-image img {
  width: 100%;
  height: 100%;
  border-radius: 20px;
}
.share .complete-item .complete-info {
  float: left;
  width: 596px;
}

.complete-item .complete-info .title {
  font: 50px Extra large;
  font-family: sans-serif;
  color: pink;
  animation: mymove 10s infinite;
}
@keyframes mymove {
  from {
    color: greenyellow;
  }
  to {
    color: pink;
  }
}

.complete-item .complete-info .sharer-info {
  margin-top: -60px;
  padding: 10px;
  margin-left: -20px;
}
.complete-item .complete-info .sharer-info span {
  font: 18px large;
  font-family: "Microsoft YaHei";
  margin-right: 5px;
}
.icon {
  font-size: 30px;
}
.complete-info .link {
  text-decoration: none;
}
.intro {
  margin-top: 40px;
  margin-left: -30px;
}
.pagination_block {
  margin-bottom: 60px;
  text-align: center;
}
.demo-form-inline {
  text-align: center;
}
</style>


